package com.bytedance.article.common.c;

import android.net.Uri;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.b.ThreadPlus;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

public class VideoLogManager {
    public static List a;
    private static VideoLogManager sInstance;
    private static List<String> mLogList;

    static {
        VideoLogManager.sInstance = null;
        new a((byte) 0).start();
        VideoLogManager.a = null;
        VideoLogManager.mLogList = new ArrayList();
    }

    private ArrayList b;

    private VideoLogManager() {
        this.b = new ArrayList();
    }

    public synchronized static VideoLogManager getInstance() {
        if (VideoLogManager.sInstance == null) {
            VideoLogManager.sInstance = new VideoLogManager();
        }
        return VideoLogManager.sInstance;
    }

    public synchronized static void appendLog(String log) {
        if (TextUtils.isEmpty(log)) {
            return;
        }
        if (VideoLogManager.mLogList.size() > 20) {
            VideoLogManager.mLogList.remove(0);
        }
        VideoLogManager.mLogList.add(log);
    }

    public synchronized static JSONArray c() {
        JSONArray v0_1;
        if (VideoLogManager.mLogList.size() > 0) {
            v0_1 = new JSONArray(VideoLogManager.mLogList);
            VideoLogManager.mLogList.clear();
        } else {
            v0_1 = null;
        }
        return v0_1;
    }

    private synchronized static JSONArray d() {
        JSONArray v0_1;

        if (VideoLogManager.a.size() > 0) {
            v0_1 = new JSONArray(VideoLogManager.a);
        } else {
            return null;
        }
        return v0_1;
    }

    public synchronized final void a(JSONObject arg7) {
        String v0_3;
        long v0_2;
        if (this.b.size() > 20) {
            this.b.remove(0);
        }

        try {
            v0_2 = System.currentTimeMillis();
            Calendar v2 = Calendar.getInstance();
            if (v0_2 >= 0) {
                v2.setTimeInMillis(v0_2);
                v0_3 = String.format("%tm-%td %tH:%tM:%tS.%tL", v2, v2, v2, v2, v2, v2);
            } else {
                v0_3 = Long.toString(v0_2);
            }
            arg7.put("pushLogTime", v0_3);
            this.b.add(arg7);
        } catch (JSONException v0_1) {
            v0_1.printStackTrace();
        }
    }

    public synchronized final String b() {
        StringBuffer v0_2;
        String v0_1 = "";
        if (this.b.size() == 0) {
            return "";
        }

        v0_2 = new StringBuffer();
        try {
            JSONObject v1_1 = new JSONObject();
            v1_1.put("logs", new JSONArray(this.b));
            JSONArray v2 = VideoLogManager.d();
            if (v2 != null) {
                v1_1.put("dns", v2);
            }

            v0_2.append(v1_1.toString());
            this.b.clear();
            v0_1 = v0_2.toString();
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
        return v0_1;
    }

    static final class a extends ThreadPlus {
        a(byte arg1) {
            this();
        }

        private a() {

        }

        public final void run() {
            ArrayList v3 = new ArrayList();
            ArrayList v0 = new ArrayList();
            ((List) v0).add("http://v4.pstatp.com/");
            ((List) v0).add("http://v5.pstatp.com/");
            ((List) v0).add("http://v6.pstatp.com/");
            ((List) v0).add("http://v7.pstatp.com/");
            ((List) v0).add("http://v9.pstatp.com/");
            ((List) v0).add("http://v10.pstatp.com/");
            Iterator v4 = ((List) v0).iterator();
            while (v4.hasNext()) {
                Object v0_1 = v4.next();
                String v1 = "";
                try {
                    Uri v5 = Uri.parse(((String) v0_1));
                    String v2 = null;
                    if (v5 != null) {
                        v2 = v5.getHost();
                    }

                    if (!android.support.design.a.isTextEmpty(v2)) {
                        v1 = InetAddress.getByName(v2).getHostAddress();
                    }
                } catch (Exception exception) {
                    v1 = exception.getMessage();
                }
                Logger.d("VideoLogManager", (((String) v0_1)) + " " + v1);
                ((List) v3).add((((String) v0_1)) + " " + v1);
            }

            synchronized (VideoLogManager.class) {
                VideoLogManager.a = ((List) v3);
            }
        }
    }
}

